<template>
	<view>
		<view class="nocart" v-if="iflogin==false">
			<text class="iconfont icon-gouwuche1" :style="'color:' + colors"></text>
			<view>尚未登陆,<text style="'color:' + colors" @tap="onStroll">前往登录 ></text></view>
		</view>
		<view class="order_list" v-if="iflogin==true">
			<view class="top_nav">
				<view style="background-color: #FFFFFF;">
					<navBar :showLeft="true" navTitle="订单列表" textcolor="#000" :leftBg="false"></navBar>
				</view>
				<!-- 顶部tab -->
				<view class="nav_top" style="border-bottom: 1upx solid #F8F8F8;">
					<tabs :colors="colors" :tabList="tabList" :active="active" @setTabs="setTabs"></tabs>
				</view>
			</view>
			<!-- 占位 -->
			<view :style="{height: 35+statusBarHeight+toBarHeight+'px'}"></view>
			<view class="list_box">
				<block v-for="(item, index) in orderList">
					<view :key="index" class="lists" v-if="item.status==status">
						<view class="type">
							<text class="order_id">订单编号:{{item.order_no}}</text>
							<text class="order_type" :style="'color:' + colors">
								{{active | setStatus}}
							</text>
						</view>
						<view v-for="(row, index2) in item.items" :key="index2" class="top" @tap="jumpDetails(item)">
							<image :src="row.goods_cover" mode="aspectFill"></image>
							<view class="top_right">
								<view class="order_name"style="display: inline-block;white-space: nowrap; overflow: hidden;text-overflow:ellipsis;width: 70%;">{{row.goods_name}}</view>
							<!-- 	<view class="sku" style="display: inline-block;white-space: nowrap; overflow: hidden;text-overflow:ellipsis;">规格：{{row.goods_spec || '暂无规格'}}</view> -->
								<view class="sku" style="display: inline-block;white-space: nowrap; overflow: hidden;text-overflow:ellipsis;width: 70%;">规格：{{row.goods_spec || '暂无规格'}}</view>
								<view class="price">
									<view class="t1">￥{{row.price_selling}}</view>
									<view class="t3">
										x{{row.stock_sales}}
									</view>
								</view>
							</view>
						</view>
						<view class="bottom">
							<view class="address" v-if="1==0">店铺地址：北京市海淀区苏家坨乡前沙涧村</view>
							<view class="btns">
								<block v-if="active == 0">
									<view class="pay"
										:style="'color:#fff;background:' + colors+ ';border-color:' + colors"
										@tap="jumpDetails(item)">去付款</view>
									<view class="pay shouhou" :style="'color:' + colors + ';border-color:' + colors"
										@tap="cencalOrder(item)">取消订单</view>
								</block>
								<block v-if="active == 1">
									<view class="pay shouhou" @tap="onRefund(item)">申请退款</view>
								</block>
								<block v-if="active == 2">
									<view class="pay"
										:style="'color:#fff;background:' + colors+ ';border-color:' + colors">确认收货
									</view>
									<view class="pay shouhou" @tap="onRefund(item)">申请退款</view>
								</block>
								<block v-if="active == 3">
									<view class="pay shouhou" :style="'color:' + colors + ';border-color:' + colors"
										@tap="jumpDetails(item)">订单评价</view>
									<view class="pay shouhou" @tap="jumpDetails(item)">申请售后</view>
								</block>
								<view class="pay shouhou" v-if="active == 4" @click="delOrder(item)">删除订单</view>
							</view>
						</view>
					</view>
				</block>
				<view class="nodata" v-if="orderList.length >= 3">—— 到底啦 ——</view>
				<nodata :colors="colors" title="暂无订单信息" v-if="orderList.length == 0"></nodata>
			</view>
		</view>
		<loading v-if="isShow == true"></loading>
	</view>
</template>

<script>
	var app = getApp();
	import tabs from "../../commponent/public/tabs";
	import loading from "../../commponent/public/loading";
	import navBar from '../../commponent/public/navBar.vue'
	import {
		getToken,
		getUserInfo
	} from '@/utils/auth.js'
	export default {
		data() {
			return {
				iflogin:true,//是否登录
				statusBarHeight: app.globalData.statusHeight,
				toBarHeight: app.globalData.toBar,
				tabList: [{
						name: '待付款',
						id: 0,
						status: 2
					}, {
						name: '待发货',
						id: 1,
						status: 4
					}, {
						name: '待收货',
						id: 2,
						status: 5
					}, {
						name: '待评价',
						id: 3,
						status: 6
					},
					{
						name: '已完成',
						id: 4,
						status: 6
					}
				],
				active: 0,
				status: 2,
				orderList: [{
						goods: [{
								title: 'DUNKINDONUTS唐恩都乐美国甜甜圈6个礼盒装 随机搭配6款',
								type: 1,
								goods_id: 201,
								number: 1,
								goods_sku_text: '醇黑巧克力【20枚】',
								img: 'http://img10.360buyimg.com/n1/jfs/t1/86401/35/12206/357766/5e43b59cE5a7aa4dd/0753be765166c195.jpg',
								money: '175.78',
							},
							{
								title: '农谣人 原味火山石烤肠1000g/约16根台式原味肠地道肠纯肉肠热狗肠台湾烤肠香肠烧烤肠半熟食火腿肠 台式原味地道肠1kg',
								type: 1,
								goods_id: 204,
								number: 1,
								goods_sku_text: '台式原味地道肠1kg',
								img: 'http://img10.360buyimg.com/n1/jfs/t1/118993/11/329/175715/5e8ac0afE94234346/3ceb1344cf34d655.jpg',
								money: '52.00 '
							},
						],
						type: 1,
						status: 0,
						order_No: 'AQWEAD45648974974456',
						shopp_Address: '北京市海淀区苏家坨乡前沙涧村'
					},
					{
						goods: [{
							title: '钟薛高 钟意你系列 特牛乳*4片 丝绒可可*4片 半巧主义*2 冰淇淋生鲜雪糕 10片装',
							img: '/static/images/goods/there.jpg',
							goods_id: 203,
							money: '152.00 ',
							sku: '',
							number: 1,
						}, ],
						type: 1,
						status: 0,
						order_No: 'AQWEAD45648974974456',
						shopp_Address: '北京市海淀区苏家坨乡前沙涧村'
					}
				],
				isShow: true,
				colors: ""
			};
		},
		filters: {
			setStatus(value) {
				if (value == 0) {
					return '待付款'
				} else if (value == 1) {
					return '待发货'
				} else if (value == 2) {
					return '待收货'
				} else if (value == 3) {
					return '待评价'
				} else if (value == 4) {
					return '已完成'
				}
			}
		},
		components: {
			tabs,
			loading,
			navBar
		},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			if (options.tabIndex) {
				this.setData({
					active: Number(options.tabIndex)
				});
			}
			if(options.status){
				this.status=Number(options.status)
			}
			this.setData({
				colors: app.globalData.newColor
			});
			setTimeout(() => {
				this.setData({
					isShow: false
				});
			}, 600);			
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {			
			if (!getToken()||(getUserInfo().expire*1000)-Date.parse(new Date())<=0) {
				this.iflogin=false;
				return
			}else{
				this.iflogin=true;
				//请求获取订单
				this.myrequest('data/api.auth.order/get', {
					'token': getToken()
				}).then((res) => {
					if (res.code == 1) {
						this.orderList = res.data.list
						console.log(this.orderList)
					}
				})
			}
		},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {},
		methods: {
			setTabs(item, index) {
				this.setData({
					active: item.id,
					status: item.status
				});
				console.log(this.status)
			},
			onStroll(){
				//登录
				uni.navigateTo({
					url: '/pages/login/login'
				})
			},
			jumpDetails(obj) { //模拟跳转商品详情
				console.log(obj)
				uni.navigateTo({
					url: '/pages/views/order/orderDetails?status=' + this.active + '&order_no=' + obj.order_no
				});
			},
			cencalOrder(item) {
				//取消订单		
				this.orderList.some((value, key) => {
					//循环找出指定数据
					if (value.status == this.status) {
						//当状态和id都相等是，请求数据库取消订单
						if (value.id == item.id) {
							let that = this;
							this.myrequest('data/api.auth.order/cancel', {
								'token': getToken(),
								order_no: item.order_no
							}, 'GET').then((res) => {
								console.log(res)
								uni.showModal({
									title: '确认要取消该订单吗?',
									confirmColor: this.colors,
									success: (res) => {
										if (res.confirm) {
											that.orderList.splice(key, 1)
											console.log('取消成功')
										}
									}
								})
							})
							return true
						}
					}
				})




			},
			onRefund(item) {
				// 申请退款
				uni.navigateTo({
					url: '/pages/views/order/cancelOrder?orderId'
				});
			},
			delOrder(item) {
				uni.showModal({
					title: '确认要删除该订单吗?',
					confirmColor: this.colors,
					success: (res) => {
						if (res.confirm) {
							console.log('删除成功')
						}
					}
				})
			}
		}
	};
</script>
<style>
	page {
		background-color: #F8F8F8;
	}
</style>
<style scoped lang="scss">
	.order_list {
		width: 100%;
		height: 100%;
		overflow: hidden;
	}

	.top_nav {
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 200;
	}

	.list_box {
		padding: 0 3%;
		box-sizing: border-box;
		overflow: hidden;
		/* #ifdef MP */
		padding-bottom: 50upx;
		/* #endif */
		width: 100%;
		padding-top: 20upx;
	}

	.lists {
		margin: 0 auto;
		padding: 20upx;
		margin-top: 5upx;
		background-color: #ffffff;
		border-radius: 10upx;
		margin-bottom: 20upx;
		box-shadow: 0upx 0upx 10upx #EDEDED;
		box-sizing: border-box;
		width: 98%;
	}

	.type {
		height: 50upx;
		font-size: 26upx;
		line-height: 50upx;
		font-weight: bold;
		text-align: right;
		display: flex;
		justify-content: space-between;
	}

	.type .order_id {
		font-weight: 600;
		color: #999;
		font-size: 24upx;
	}

	.lists .top {
		display: flex;
		align-content: center;
		margin-bottom: 20upx;
	}

	.lists .top image {
		min-width: 180upx;
		max-width: 180upx;
		height: 180upx;
		display: block;
		border-radius: 10upx;
	}

	.lists .top .top_right {
		margin-left: 20upx;
		width: 100%;
	}

	.lists .top .order_name {
		line-height: 40upx;
		height: 60upx;
		font-size: 26upx;
		font-weight: bold;
		color: #333;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
	}

	.lists .top .sku {
		height: 40upx;
		line-height: 40upx;
		font-size: 24upx;
		border-radius: 8upx;
		padding: 0 10upx;
		color: #A7A7A7;
		background-color: #F7F7F7;
	}

	.lists .top .price {
		width: 100%;
		display: flex;
		align-items: center;
		height: 40upx;
		line-height: 40upx;
		margin-top: 20upx;
		position: relative;
	}

	.price .t1 {
		font-size: 30upx;
		// font-weight: bold;
		display: block;
		color: $mycolor;
	}

	.price .t2 {
		font-size: 24upx;
		margin-left: 15upx;
		display: block;
		color: #C5C5C5;
		text-decoration: line-through;
	}

	.price .t3 {
		float: right;
		font-weight: bold;
		font-size: 28upx;
		color: #999;
		position: absolute;
		right: 0upx;
		top: 0;
	}

	.list_box .bottom {
		margin-top: 20upx;
	}

	.list_box .bottom .address {
		font-size: 24upx;
		color: #696969;
	}

	.list_box .bottom .btns {
		margin-top: 15upx;
		overflow: hidden;
	}

	.list_box .bottom .btns .pay {
		padding: 8upx 15upx;
		border-radius: 8upx;
		text-align: center;
		font-size: 24upx;
		float: right;
		margin-left: 20upx;
		box-sizing: border-box;
		border: 1upx solid #ddd;
	}

	.pay:active {
		opacity: .8;
	}

	.nodata {
		color: #ccc;
		text-align: center;
		font-size: 24upx;
		margin-bottom: 30upx;
		height: 80upx;
		line-height: 80upx;
	}
	.nocart {
		text-align: center;
		margin-top: 30vh;
	}
	
	.nocart .iconfont {
		font-size: 80rpx;
		text-align: center;
	}
	
	.nocart view {
		height: 40rpx;
		line-height: 40rpx;
		font-size: 24rpx;
		color: #999;
		margin-top: 20rpx;
	}
</style>
